using System;
using System.Diagnostics.CodeAnalysis;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using HeroldIT.Dnn.Modules.WebControls;

namespace HeroldIT.Dnn.Modules.News.Views
{
    public abstract class SettingsBase : ModuleSettingsBase, INewsSatelliteModuleSettingsProvider
    {
        #region control fields

        protected LocalizedModuleTitleControl lmtModuleTitle;
        protected SkinSelector sksSkinUrl;
        protected ModuleSelector mdsNewsCoreModuleId;

        #endregion

        #region properties

        private NewsController controller;

        protected NewsController Controller
        {
            get
            {
                if (null == this.controller)
                {
                    this.controller = new NewsController(this);
                }
                return this.controller;
            }
        }

        protected abstract string SkinUrl { get; set; }

        #endregion

        #region page lifecycle

        protected override void OnInit(EventArgs e)
        {
            if (null != this.sksSkinUrl)
            {
                this.sksSkinUrl.Module = this.ModuleConfiguration;
            }

            base.OnInit(e);
        }

        #endregion

        #region methods

        /// <summary>
        /// Loads the module's settings into the given controls.
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public override void LoadSettings()
        {
            try
            {
                if (!this.Page.IsPostBack)
                {
                    if (null != this.sksSkinUrl)
                    {
                        this.sksSkinUrl.SkinPath = this.SkinUrl;
                    }
                    if (null != this.mdsNewsCoreModuleId)
                    {
                        this.mdsNewsCoreModuleId.ModuleId = this.GetNewsCoreModuleId();
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// <summary>
        /// Updates the module's settings from the given controls.
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public override void UpdateSettings()
        {
            try
            {
                if (null != this.lmtModuleTitle)
                {
                    this.lmtModuleTitle.Update();
                }
                if (null != this.sksSkinUrl)
                {
                    this.SkinUrl = this.sksSkinUrl.SkinPath;
                }
                if (null != this.mdsNewsCoreModuleId)
                {
                    this.SetNewsCoreModuleId(this.mdsNewsCoreModuleId.ModuleId);
                }

                //refresh cache
                ModuleController.SynchronizeModule(this.ModuleId);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// <summary>
        /// Gets the localized string for the given key.
        /// </summary>
        /// <param name="key">The key for which to find the localized string.</param>
        /// <returns>The localized string for the given key.</returns>
        protected string GetString(string key)
        {
            return Localization.GetString(key, this.LocalResourceFile);
        }

        protected string GetString(string key, string defaultText)
        {
            string localizedResource = this.GetString(key);
            if (String.IsNullOrEmpty(localizedResource))
            {
                return defaultText;
            }
            return localizedResource;
        }

        #endregion
    }
}